home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17850 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  62 lines

  1. Path: risc.sps.mot.com!not-for-mail
  2. From: moss@sit.sps.mot.com (Matthew Moss)
  3. Newsgroups: comp.lang.c++
  4. Subject: Incompatible function pointers?
  5. Date: 17 Apr 1996 14:11:33 -0500
  6. Organization: Motorola, Inc. -- Austin,TX
  7. Message-ID: <4l3fp5INN1ak@sit.sps.mot.com>
  8. NNTP-Posting-Host: sit.sps.mot.com
  9.  
  10.  
  11. Can anyone tell me why this won't work? All compilers I've tried complain
  12. about passing G::Create() into test().... I would've thought this a problem
  13. if they were member functions, but G::Create() is static.
  14.  
  15. I couldn't easily find anything about this in the ARM or ANSI references...
  16. Thanks...
  17.  
  18. class A {
  19.  public:
  20.   int x;
  21.  
  22.   A(int v = 0) : x(v) { }
  23. };
  24.  
  25. class G {
  26.   int y;
  27.  
  28.   G(int v = 0) : y(v) { }
  29.  
  30.  public:
  31.   static G* Create(A*);
  32. };
  33.  
  34.  
  35. typedef void* (*CreatorFP)(A*);
  36.  
  37.  
  38. G* G::Create(A *a)
  39. {
  40.   return new G(a->x);
  41. }
  42.  
  43.  
  44. void* test(A *a, CreatorFP cfp)
  45. {
  46.   return (*cfp)(a);
  47. }
  48.  
  49.  
  50. void main()
  51. {
  52.   A *a = new A(65);
  53.   G *g = (G*) test(a, G::Create);
  54.  
  55.   delete g;
  56.   delete a;
  57. }
  58. -- 
  59. ===============================================================================
  60.  Matthew D Moss                                        RISC Software, Motorola
  61.  moss@risc.sps.mot.com                             http://www.mot.com/PowerPC/
  62.